package com.caifu.controller;


import com.caifu.bean.Result;
import com.caifu.pojo.COrg;
import com.caifu.pojo.SysUser;
import com.caifu.service.CDeviceService;
import com.caifu.service.COrgService;
import com.caifu.util.Utils;
import com.caifu.util.tools.DateUtils;
import com.caifu.vo.CorgVo;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 机构用户表 前端控制器
 * </p>
 *
 * @author tww
 * @since 2020-12-10
 */
@Controller
@RequestMapping("org")
public class COrgController extends AbstractController {

    final static Logger logger = LoggerFactory.getLogger(COrgController.class);

    @Resource
    private COrgService cOrgService;

    @Resource
    private CDeviceService deviceService;

    private String pre = "view/user/";

    /**
     * 跳机构列表页
     */
    @GetMapping("toOrgListPage")
    public String toOrgListPage() {
        return pre + "user_list";
    }

    /**
     * 机构添加页
     */
    @GetMapping("toAddOrgPage")
    public String toAddOrgPage() {
        return pre + "user_add";
    }


    /**
     * 跳机构详情页
     */
    @GetMapping("toOrgDetailsPage")
    public String toOrgDetailsPage(String id, Model model) {
        try {
            //获取机构详情
            COrg org = cOrgService.getOrgById(id);
            //账号
            String userId = cOrgService.getorgUser(id);
            model.addAttribute("userId", userId);
            model.addAttribute("startTime", DateUtils.simpleDateFormat(DateUtils.dateFor(org.getStartTime())));
            model.addAttribute("overdueTime", DateUtils.simpleDateFormat(DateUtils.dateFor(org.getOverdueTime())));
            model.addAttribute("org", org);
            //可用设备
            model.addAttribute("id", id);
            return pre + "user_details";
        } catch (Exception e) {
            e.printStackTrace();
            return "404";
        }
    }

    /**
     * 获取机构所有设备
     */
    @GetMapping("getDevByOrgId")
    @ResponseBody
    public Result getDevByOrgId(Integer id) {
        //获取机构详情
        try {
            Map map = new HashMap<>();
            map.put("orgId", id);
            return success(200, "", deviceService.listOrgDevice(map));
        } catch (Exception e) {
            String msg = "网络异常";
            if (Utils.isNotNull(e.getMessage())) {
                msg = e.getMessage();
            }
            logger.error(e.toString());
            e.printStackTrace();
            return error(500, msg);
        }
    }


    /**
     * 获取机构列表 分页
     */
    @GetMapping("getOrgList")
    @ResponseBody
    public Result getOrgList(CorgVo corgVo) {
        try {
            PageInfo<CorgVo> pageInfo = cOrgService.getOrgList(corgVo);
            return success(0, "", pageInfo);
        } catch (Exception e) {
            String msg = "网络异常";
            if (Utils.isNotNull(e.getMessage())) {
                msg = e.getMessage();
            }
            logger.error(e.toString());
            e.printStackTrace();
            return error(500, msg);
        }
    }

    /**
     * 添加机构和机构登录账号
     */
    @PostMapping("addOrgs")
    @ResponseBody
    public Result addOrUpdate(COrg cOrg) {
        SysUser user = getUser();
        try {
            return cOrgService.addOrUpdate(cOrg, user);
        } catch (Exception e) {
            String msg = "网络异常";
            if (Utils.isNotNull(e.getMessage())) {
                msg = e.getMessage();
            }
            logger.error(e.toString());
            e.printStackTrace();
            return error(500, msg);
        }
    }

    /**
     * 根据id查询机构信息
     * id 机构id
     */
    @GetMapping("getOrgById")
    @ResponseBody
    public Result getOrgById(String id) {
        try {
            return success(200, "", cOrgService.getOrgById(id));
        } catch (Exception e) {
            String msg = "网络异常";
            if (Utils.isNotNull(e.getMessage())) {
                msg = e.getMessage();
            }
            logger.error(e.toString());
            e.printStackTrace();
            return error(500, msg);
        }
    }

    /**
     * 机构重置密码
     */
    @PostMapping("resetPwd")
    @ResponseBody
    public Result resetPwd(Integer id) {
        try {
            return cOrgService.resetPwd(id);
        } catch (Exception e) {
            String msg = "网络异常";
            if (Utils.isNotNull(e.getMessage())) {
                msg = e.getMessage();
            }
            logger.error(e.toString());
            e.printStackTrace();
            return error(500, msg);
        }
    }

    /**
     * 机构停用
     * <p>
     * flag 1 停用 2 启用
     * id机构id
     */
    @PostMapping("orgStop")
    @ResponseBody
    public Result orgStop(String flag, Integer id) {
        try {
            return cOrgService.orgStop(flag, id);
        } catch (Exception e) {
            String msg = "网络异常";
            if (Utils.isNotNull(e.getMessage())) {
                msg = e.getMessage();
            }
            logger.error(e.toString());
            e.printStackTrace();
            return error(500, msg);
        }
    }


    /**
     * 获取可选机构
     */
    @GetMapping("loadOptionalOrg")
    @ResponseBody
    public Result getOptionalOrg(String deviceId) {
        SysUser user = getUser();
        try {
            return success(0, "", cOrgService.getOptionalDept());
        } catch (Exception e) {
            String msg = "网络异常";
            if (Utils.isNotNull(e.getMessage())) {
                msg = e.getMessage();
            }
            logger.error(e.toString());
            e.printStackTrace();
            return error(500, msg);
        }
    }

    /**
     * 获取已选机构
     */
    @GetMapping("loadSelectedOrg")
    @ResponseBody
    public Result getSelectedOrg(String devId) {
        SysUser user = getUser();
        try {
            return success(0, "", cOrgService.getSelectedOrg(devId));
        } catch (Exception e) {
            String msg = "网络异常";
            if (Utils.isNotNull(e.getMessage())) {
                msg = e.getMessage();
            }
            logger.error(e.toString());
            e.printStackTrace();
            return error(500, msg);
        }
    }

    /**
     * 获取已选机构
     */
    @GetMapping("test")
    public String test() {
        return "view/device/add";
    }


}
